<template>
  <el-dialog width="66%" :title="dialogTitle" v-model="dialogVisible" modal @close="closeDialog" draggable
    :close-on-click-modal="false" :append-to-body="true">
    <el-table :data="tableData" border style="width: 100%" height="30vh">
      <el-table-column v-for="item of tableProps" :key="item.props" :prop="item.prop" :label="item.title"
        :width="item.width">
      </el-table-column>
      <el-table-column label="操作" min-width="60">
        <template slot-scope="{ row }">
          <el-popconfirm title="确定要删除吗？" @confirm="() => handleDel(row)">
            <el-button slot="reference" icon="el-icon-delete" link size="mini" class="danger_button">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false" size="mini">取 消</el-button>
      <el-button type="primary" @click="submitForm" size="mini">下 架</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { archivesDown, boxDown } from '@/api/rfid/downBill'
export default {
  name: 'downShelvesDialog',
  data() {
    return {
      type: '',
      dialogTitle: '',
      dialogVisible: false,
      formData: {},
      tableData: [],
      tableProps: [],
    }
  },
  created() { },
  methods: {
    add(record, type) {
      this.type = type
      if (type === 'box') {
        this.tableProps = [
          { title: '盒号', prop: 'boxNo', width: '', checked: true },
          { title: '盒名', prop: 'boxName', width: '', checked: true },
          { title: 'RFID', prop: 'rfid', width: '', checked: true },
          { title: '盒宽(CM)', prop: 'boxWidth', width: '', checked: true },
        ]
      } else if (type === 'archive') {
        this.tableProps = [
          { title: '档号', prop: 'archiveNo', width: '', checked: true },
          { title: '题名', prop: 'archiveName', width: '', checked: true },
          { title: 'RFID', prop: 'rfid', width: '', checked: true },
          { title: '页数', prop: 'pageNo', width: '', checked: true },
        ]
      }
      this.tableData = record
      this.dialogTitle = '添加到下架列表'
      // 先清空表单数据
      this.formData = {}
      this.dialogVisible = true
    },
    handleDel(row) {
      this.tableData = this.tableData.filter((item) => item.labelId !== row.labelId)
    },
    submitForm() {
      // 判断表格中是否还有数据
      if (this.tableData.length === 0) {
        this.$message.warning('下架列表不能为空')
        return
      }
      this.formData.labelIds = this.tableData.map((item) => item.labelId)
      if (this.type === 'box') {
        this.formData.dataSources = 0
        boxDown(this.formData)
          .then((res) => {
            if (res.data.code === 200) {
              this.$message.success('下架成功')
              this.closeDialog()
              this.$emit('ok', this.type, this.tableData)
              this.$emit('update-data')
              this.$emit('reloadTable')
            } else {
              this.$message.warning(res.data.message)
            }
          })
          .catch((err) => {
            this.$message.error(err)
          })
      } else if (this.type === 'archive') {
        this.formData.dataSources = 0
        archivesDown(this.formData)
          .then((res) => {
            if (res.data.code === 200) {
              this.$message.success('下架成功')
              this.closeDialog()
              this.$emit('ok', this.type, this.tableData)
              this.$emit('update-data')
              this.$emit('reloadTable')
            } else {
              this.$message.warning(res.data.message)
            }
          })
          .catch((err) => {
            this.$message.error(err)
          })
      }
    },
    closeDialog() {
      this.dialogVisible = false
    },
  },
}
</script>

<style scoped></style>
